<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改记录跟踪记录弹出页面</title>

    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../styles/layui-alter.css">
    <link rel="stylesheet" href="../styles/center.css">

</head>
<body>
<div class="m-center-10">
    <form method="post" action="/static" class="layui-form" lay-filter="update-track-form">
        <div class="layui-form-item">
            <label class="layui-form-label">托运单号：</label>
            <div class="layui-input-inline">
                <input type="text" name="trackConsignNum" class="layui-input input-m" lay-verify="required">
            </div>
            <label class="layui-form-label">更新日期：</label>
            <div class="layui-input-inline">
                <input type="text" name="trackDate" id="track-date" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">跟踪类型：</label>
            <div class="layui-input-inline">
                <select name="trackType" lay-verify="required">
                    <option value="">请选择跟踪类型</option>
                    <option value="1">收件</option>
                    <option value="2">装车</option>
                    <option value="3">运输</option>
                    <option value="4">转运</option>
                    <option value="5">派送</option>
                    <option value="6">签收</option>
                </select>
            </div>

            <label class="layui-form-label">记录站点：</label>
            <div class="layui-input-inline">
                <select name="trackRecordSubst" lay-verify="required">
                    <option value="">请选择站点</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">跟踪状态：</label>
            <div class="layui-input-block">
                <input type="text"  name="trackStatus" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" style="margin:10% 30% 0 30%;" class="layui-btn pool-blue btn-r" lay-submit lay-filter="updateTrack">保存记录</button>
                <input type="hidden" name="trackId" />
            </div>
        </div>
    </form>
</div>

<script src="../layui/layui.js"></script>
<script>
    layui.use(['layer','element','form', 'laydate', 'jquery'],function () {
        var layer = layui.layer;
        var element = layui.element;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;


        var trackId = window.sessionStorage.getItem("trackId");
        window.sessionStorage.removeItem("trackId");
        $("input[name='trackId']").val(trackId);

        laydate.render({ //跟踪记录日期
            elem: '#track-date', //指定元素
            position: 'fixed',
            type: 'datetime',
            theme: "#5974d9"
        });
        //加载跟踪记录分站
        loadSubstList("trackRecordSubst");

        //加载跟踪记录数据
        $.ajax({
            url: "/queryOneTrackById",
            async: false,
            type: "POST",
            dataType: 'json',
            data: {
                trackId: trackId
            },
            success:function (data) {
                console.log(data);

                //加载框缓冲
                layer.load(1, {
                    shade: [0.5, '#fff'], //0.1透明度的白色背景
                    time: 1000,
                    end: function () {

                        $("select[name='trackRecordSubst']").val(data['trackRecordSubst']);
                        //表单赋值
                        form.val("update-track-form",{
                            trackConsignNum: data['trackConsignNum'],
                            trackDate: data['trackDate'],
                            trackType: data['trackType'],
                            trackRecordSubst: data['trackRecordSubst'],
                            trackStatus: data['trackStatus'],
                            trackId: trackId
                        });
                        form.render('select');
                        //新增跟踪记录监听提交
                        form.on('submit(updateTrack)', function(data){
                            console.log(data.field);

                            $.ajax({
                                url:"/updateTrackById",
                                type: "POST",
                                dataType:'json',
                                contentType: "application/json;charset=UTF-8",
                                data: JSON.stringify(data.field),
                                success:function(msg){
                                    console.log(msg);
                                    layer.msg(msg['msg'],
                                        {
                                            icon: 6,
                                            time: 1400,
                                            end:function () {
                                                if (msg['result'] == "success") {
                                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                    parent.layer.close(index); //再执行关闭
                                                } else {

                                                }
                                            }
                                        }
                                    );

                                }
                            });
                            return false;
                        });
                        form.render();
                    }
                });
            }
        });



        /**
         * 加载--分站--下拉框列表的方法
         * @param selectName 下拉框name,可以是str类型和arr类型
         */
        function loadSubstList(selectName) {
            $.ajax({
                type: "POST",
                url: "/getSubstationListFS",
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var li = "<option value='" + data[i]['substId'] + "'>" + data[i]['substName'] + "</option>"

                        if (typeof selectName == "string") {
                            $("select[name='" + selectName + "']").append(li);
                        } else if (typeof selectName != "undefined") {
                            for (var j = 0; j < selectName.length; j++) {
                                $("select[name='" + selectName[j] + "']").append(li);
                            }
                        }
                    }
                    form.render("select");
                }
            });
        }
    })
</script>
</body>
</html>